package cn.pconline.autoclub.web;

import cn.pconline.autoclub.entity.Theme;
import cn.pconline.autoclub.service.ThemeService;
import cn.pconline.autoclub.util.AppUtils;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zhouruxuan
 * @date 2014年10月16日 上午11:12:06
 */
@Controller
public class ThemeController {

    @Autowired
    private ThemeService themeService;
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(ThemeController.class);

    /**
     * 返回相关Json信息
     *
     * @param resp
     * @param str
     * @param callback
     */
    private void printMsg(HttpServletResponse resp, String str, String callback) {
        callback = EncodeUtils.encodeForHTMLFilterNull(callback);
        try {
            java.io.PrintWriter w = resp.getWriter();
            if (StringUtils.isNotBlank(callback)) {
                w.print(callback + "(" + str + ")");
            } else {
                w.print(str);
            }
            w.flush();
            w.close();
        } catch (IOException ex) {
            log.error("response输出出错了", ex);
        }
    }

    /**
     * 主题列表（APP）
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/club/listAllTheme.do", method = RequestMethod.GET)
    public void listAllTheme(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding(request, response);
        //获取相关参数
        String callback = WebUtils.param(request, "callback", "");
        int pageSize = WebUtils.paramInt(request, "pageSize", 20);
        int pageNo = WebUtils.paramInt(request, "pageNo", 1);
        JSONArray jsonArray = new JSONArray();
        JSONObject json = new JSONObject();

        Pager<Theme> pager = themeService.listAllThemePager(pageNo, pageSize);
        if (pager != null) {
            for (Theme theme : pager.getResultList()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("clubTagId", theme.getThemeId());//主题ID
                jsonObject.put("clubTagName", theme.getThemeName());//主题名字
                jsonArray.add(jsonObject);
            }
        }
        json.put("data", jsonArray);
        json.put("total", pager.getTotal());
        json.put("pageNo", pager.getPageNo());
        json.put("pageSize", pager.getPageSize());
        json.put("code", AppUtils.STATUS_OK);
        json.put("message", "成功获取主题列表！");
        printMsg(response, json.toString(), callback);
    }
}
